package com.kaiwukj.android.ufamily.mvp.ui.page.mine.house.door;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.blankj.utilcode.util.GsonUtils;
import com.blankj.utilcode.util.TimeUtils;
import com.kaiwukj.android.ufamily.R;
import com.kaiwukj.android.ufamily.app.base.review.BaseActivity;
import com.kaiwukj.android.ufamily.app.constant.ARouterUrlKt;
import com.kaiwukj.android.ufamily.app.constant.Constant;
import com.kaiwukj.android.ufamily.mvp.http.entity.helper.DoorTempPassResult;
import com.kaiwukj.android.ufamily.utils.ClipboardUtils;
import com.tencent.mm.opensdk.modelmsg.SendMessageToWX;
import com.tencent.mm.opensdk.modelmsg.WXMediaMessage;
import com.tencent.mm.opensdk.modelmsg.WXTextObject;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;
import com.zghl.mclient.client.ZghlMClient;
import com.zghl.mclient.client.ZghlStateListener;

import butterknife.BindView;
import butterknife.OnClick;

@Route(path = ARouterUrlKt.DoorAccessPassUrl)
public class DoorAccessPassActivity extends BaseActivity {

    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_layout_title)
    TextView tvLayoutTitle;
    @BindView(R.id.tv_access_pass)
    TextView tvAccessPass;
    @BindView(R.id.tv_valid_date)
    TextView tvValidDate;

    @Autowired
    String roomId;
    @Autowired
    String roomName;

    private DoorTempPassResult passResult;
    private IWXAPI mWxApi;

    @OnClick(R.id.container_left)
    void onBackClick() {
        onBackPressedSupport();
    }

    @OnClick({R.id.container_sms, R.id.container_wechat})
    void onShareEvent(View view) {
        if (passResult == null) {
            showMessage("无法获取开门密码");
            return;
        }
        String passInfo = getPassInfo();
        switch (view.getId()) {
            case R.id.container_sms:
                shareToSms(passInfo);
                break;
            case R.id.container_wechat:
                shareToWechat(passInfo);
                break;
        }
    }

    @OnClick(R.id.btn_copy)
    void onCopyInfo() {
        if (passResult != null) {
            ClipboardUtils.copyText(getPassInfo());
            showMessage("复制成功");
        }
    }

    @Override
    protected int getLayoutId() {
        ARouter.getInstance().inject(this);
        return R.layout.activity_door_access_pass;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        tvTitle.setVisibility(View.GONE);
        tvLayoutTitle.setText("获取密码");
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        ZghlMClient.getInstance().getTempPassword(roomId, new ZghlStateListener() {
            @Override
            public void onSuccess(int i, String s) {
                try {
                    passResult = GsonUtils.fromJson(s, DoorTempPassResult.class);
                    tvValidDate.setText(String.format("有效期至 %s", TimeUtils.millis2String(passResult.getPwd_expired() * 1000, "yyyy-MM-dd HH:mm")));
                    tvAccessPass.setText(String.valueOf(passResult.getPwd_value()));
                } catch (Exception e) {
                    showMessage(e.getLocalizedMessage());
                }
            }

            @Override
            public void onError(int i, String s) {
                showMessage(s);
            }
        });
        //tvAccessPass.setText(pass);
    }

    private void shareToSms(String shareText) {
        Intent smsIntent = new Intent(Intent.ACTION_SEND);
        smsIntent.setType("text/plain");
        smsIntent.putExtra(Intent.EXTRA_SUBJECT, "分享");
        smsIntent.putExtra(Intent.EXTRA_TEXT, shareText);
        startActivity(smsIntent);
    }

    private void shareToWechat(String shareText) {
        WXTextObject textObject = new WXTextObject();
        textObject.text = shareText;

        WXMediaMessage msg = new WXMediaMessage();
        msg.mediaObject = textObject;
        msg.description = shareText;

        SendMessageToWX.Req req = new SendMessageToWX.Req();
        req.transaction = "jyl_zghl_pass";
        req.message = msg;
        req.scene = SendMessageToWX.Req.WXSceneSession;

        if (mWxApi == null) {
            mWxApi = WXAPIFactory.createWXAPI(this, null);
            mWxApi.registerApp(Constant.WX_APP_ID);
        }
        if (!mWxApi.isWXAppInstalled()) {
            showMessage(getString(R.string.please_install_wechat));
            return;
        }
        mWxApi.sendReq(req);

    }

    //访客密码：730641,可开启【青春家园小区1栋1单元102】关联门禁,该访客密码可在每台门禁机上使用2次，截止至2020-06-02 21:55 有效,在门禁机上点击”密码开门”，再输入密码直接开门
    private String getPassInfo() {
        return String.format("访客密码：%s,可开启【%s%s】关联门禁,该访客密码可在每台门禁机上使用%s次，截止至%s 有效,在门禁机上点击”密码开门”，再输入密码直接开门",
                passResult.getPwd_value(),
                getApp().getUserInfo().getLastCommunityName(), roomName,
                passResult.getPwd_limit(),
                TimeUtils.millis2String(passResult.getPwd_expired() * 1000, "yyyy-MM-dd HH:mm"));
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mWxApi != null) {
            mWxApi.unregisterApp();
            mWxApi.detach();
        }
    }
}
